#include "prec.h"
#include "artstatictext.h"
#include "artpanel.h"
#include "wx/log.h"
#include "wx/dcbuffer.h"

IMPLEMENT_DYNAMIC_CLASS(ArtStaticText, wxControl)

BEGIN_EVENT_TABLE(ArtStaticText, wxControl)
	EVT_PAINT(ArtStaticText::OnPaint)
	EVT_SIZE(ArtStaticText::OnSize)
	EVT_ERASE_BACKGROUND(ArtStaticText::OnEraseBackground)
END_EVENT_TABLE()


ArtStaticText::ArtStaticText( ArtPanel *parent, wxWindowID id, 
							 const wxString& label, 
							 const wxPoint& pos /*= wxDefaultPosition*/, 
							 const wxSize& size /*= wxDefaultSize*/, 
							 long style /*= 0*/, 
							 const wxString& name /*= wxStaticTextNameStr*/ )
							 : wxControl(parent, id, pos, size, style, wxDefaultValidator, name)
							 , m_artPanel(parent)
{
	wxControl::SetLabel(label);

	InheritAttributes();
	SetInitialSize(size);
}

bool ArtStaticText::SetFont( const wxFont& font )
{
	bool ret = wxControl::SetFont(font);
	Refresh();
	return ret;
}

void ArtStaticText::SetLabel( const wxString& label )
{
	wxControl::SetLabel(label);
	Refresh();
}

bool ArtStaticText::SetForegroundColour( const wxColour& colour )
{
	bool ret = wxControl::SetForegroundColour(colour);
	Refresh();
	return ret;
}

void ArtStaticText::OnPaint( wxPaintEvent& event )
{
	wxBufferedPaintDC dc(this);

	dc.SetBackground(wxBrush(GetParent()->GetBackgroundColour()));
	dc.Clear();

 	wxBitmap bmp = m_artPanel->GetBackgroundRegion(GetId());
 	if (bmp.Ok())
 	{
 		dc.DrawBitmap(bmp, 0, 0, false);
 	}

	dc.SetTextForeground(GetForegroundColour());
	dc.SetFont(GetFont());

	wxString label = GetLabel();
	wxSize fsize = dc.GetTextExtent(label);
	dc.DrawText(label, 0, 0);
}

wxSize ArtStaticText::DoGetBestSize() const
{
	wxString label = GetLabel();
	if (label.IsEmpty())
		return wxSize(112, 48);

	wxClientDC dc((wxWindow*)this);
	dc.SetFont(GetFont());
	return dc.GetTextExtent(label);
}

void ArtStaticText::OnEraseBackground( wxEraseEvent& event )
{

}

void ArtStaticText::OnSize( wxSizeEvent& event )
{
	event.Skip();
	Refresh();
}